package com.redoor.app.stake.workflow.controller;

import com.github.pagehelper.PageInfo;
import com.redoor.app.stake.basicdata.commons.BasicDataStatus;
import com.redoor.app.stake.basicdata.dao.BaseStakeMapper;
import com.redoor.app.stake.basicdata.hsf.service.impl.StakeServiceImpl;
import com.redoor.app.stake.basicdata.model.BaseStake;
import com.redoor.app.stake.basicdata.service.BaseStakeService;
import com.redoor.app.stake.workflow.commons.WorkFlowStatus;
import com.redoor.app.stake.workflow.model.controllerModel.WorkOrderCriteria;
import com.redoor.app.stake.workflow.model.serviceModel.BaseWorkOrderServiceBean;
import com.redoor.app.stake.workflow.model.serviceModel.WorkOrderNumBean;
import com.redoor.app.stake.workflow.service.WorkOrderService;
import com.redoornetwork.framework.core.response.ResponseResult;
import com.redoornetwork.framework.core.response.ResultGenerator;
import com.yotouch.app.stake.StakeConsts;
import com.yotouch.app.stake.model.response.ResponseBase;
import com.yotouch.app.stake.model.response.ResponseStake;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
* Created by wangjunlei on 2018-03-07 17:06:26.
*/
@RestController
@RequestMapping("/base/workorders")
public class BaseWorkorderController {
    private final Logger logger = LoggerFactory.getLogger(BaseWorkorderController.class);

    @Autowired
    private WorkOrderService workOrderService;

    /**
     *  工单已完成列表
     *
     * @author ZhangMumu
     * @date 2018/3/31 0031 16:13
     * @param
     * @return
     */
    @ApiOperation(value = "已完成工单列表")
    @PostMapping("/donelist")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "limit", value = "偏移量", paramType = "query", dataType = "int", required = true, defaultValue = "20"),
            @ApiImplicitParam(name = "offset", value = "起始记录", paramType = "query", dataType = "int", required = true, defaultValue = "1")
    })
    public ResponseResult donList(WorkOrderCriteria workOrderCriteria){
        try {
            List<BaseWorkOrderServiceBean> doneList = workOrderService.qryDoneWorkOrder(workOrderCriteria);
            Integer doneListNum = workOrderService.qryDoneWorkOrderNum(workOrderCriteria);
            PageInfo pageInfo = new PageInfo();
            pageInfo.setList(doneList);
            pageInfo.setTotal(doneListNum);
            return ResultGenerator.generatorSuccess(pageInfo);
        }catch (Exception e){
            logger.error("query_done_list_error,time:"+ new Date() + "; content:" + e.getMessage());
            e.printStackTrace();
            return ResultGenerator.generatorFail(WorkFlowStatus.OTHER_EXCEPTION);
        }
    }
    /**
     *  查询待办列表
     *
     * @author ZhangMumu
     * @date 2018/3/31 0031 16:30
     * @param
     * @return
     */
    @ApiOperation(value = "待办工单列表")
    @PostMapping("/todoList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "limit", value = "偏移量", paramType = "query", dataType = "int", required = true, defaultValue = "20"),
            @ApiImplicitParam(name = "offset", value = "起始记录", paramType = "query", dataType = "int", required = true, defaultValue = "1")
    })
    public ResponseResult todoList(WorkOrderCriteria workOrderCriteria){
        try{
            List<BaseWorkOrderServiceBean> todoList = workOrderService.qryTodoWorkOrder(workOrderCriteria);
            Integer todoListNum = workOrderService.qryTodoWorkOrderNum(workOrderCriteria);
            PageInfo  pageInfo = new PageInfo();
            pageInfo.setList(todoList);
            pageInfo.setTotal(todoListNum);
            return ResultGenerator.generatorSuccess(pageInfo);
        }catch (Exception e){
            logger.error("query_todo_list_error,time:"+ new Date() + "; content:" + e.getMessage());
            return ResultGenerator.generatorFail(WorkFlowStatus.OTHER_EXCEPTION);
        }
    }
    /**  
     *  注册码审核通过
     *   
     * @author ZhangMumu  
     * @date 2018/3/31 0031 17:02  
     * @param   workOrderCriteria
     * @return   
     */
    @ApiOperation(value = "注册码审核通过,参数:查询用的实体")
    @PostMapping("/registrationApplyAccept")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uuid", value = "工单uuid", dataType = "string", paramType = "query", required = true),
            @ApiImplicitParam(name = "reviewSuggestion", value = "审核时填写的备注", dataType = "string", paramType = "query", required = true),
            @ApiImplicitParam(name = "acceptedAmount", value = "注册码审核时候同意下发注册码数量", dataType = "int", paramType = "query", required = true)
    })
    public ResponseResult registrationApplyAccept(WorkOrderCriteria workOrderCriteria){
        if (null == workOrderCriteria){
            return ResultGenerator.generatorFail(WorkFlowStatus.PARAM_IS_EMPTY);
        }
        if (0 < workOrderService.reApplyAccept(workOrderCriteria)){
            return ResultGenerator.generatorSuccess();
        }else{
            return ResultGenerator.generatorFail(WorkFlowStatus.OTHER_EXCEPTION);
        }
    }
    /**
     *  注册码审核拒绝
     *
     * @author ZhangMumu
     * @date 2018/3/31 0031 17:06
     * @param workOrderCriteria
     * @return
     */
    @ApiOperation(value = "注册码审核拒绝,参数:查询用的实体")
    @PostMapping("/registrationApplyReject")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uuid", value = "工单uuid", dataType = "string", paramType = "query", required = true),
            @ApiImplicitParam(name = "reviewSuggestion", value = "审核时填写的备注", dataType = "string", paramType = "query", required = true),
            @ApiImplicitParam(name = "acceptedAmount", value = "注册码审核时候同意下发注册码数量", dataType = "int", paramType = "query", required = true)
    })
    public ResponseResult registrationApplyReject(WorkOrderCriteria workOrderCriteria){
        if(null == workOrderCriteria){
            return ResultGenerator.generatorFail(WorkFlowStatus.PARAM_IS_EMPTY);
        }
        if (0 < workOrderService.reApplyReject(workOrderCriteria)){
            return ResultGenerator.generatorSuccess();
        }else{
            return ResultGenerator.generatorFail(WorkFlowStatus.OTHER_EXCEPTION);
        }

    }
    /**
     *  桩维护工单提交
     *
     * @author ZhangMumu
     * @date 2018/3/31 0031 17:11
     * @param workOrderUuid 工单uuid
     * @return
     */
    @ApiOperation(value = "桩维护工单提交,参数:工单uuid")
    @PostMapping("/maApplySubmit")
    public ResponseResult maApplySubmit(String workOrderUuid){
        if (StringUtils.isBlank(workOrderUuid)){
            return ResultGenerator.generatorFail(WorkFlowStatus.PARAM_IS_EMPTY);
        }
        if (0 < workOrderService.maApplySubmit(workOrderUuid)){
            return ResultGenerator.generatorSuccess();
        }else{
            return ResultGenerator.generatorFail(WorkFlowStatus.OTHER_EXCEPTION);
        }
    }
    /**
     *  桩维护审核通过
     *
     * @author ZhangMumu
     * @date 2018/3/31 0031 17:14
     * @param workOrderCriteria 工单uuid
     * @return
     */
    @ApiOperation(value = "桩维护审核通过,参数:工单uuid")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uuid", value = "工单uuid", dataType = "string", paramType = "query", required = true),
            @ApiImplicitParam(name = "reviewSuggestion", value = "审核时填写的备注", dataType = "string", paramType = "query")
    })
    @PostMapping("/maChargeAccept")
    public ResponseResult maChargeAccept(WorkOrderCriteria workOrderCriteria){
        if (null == workOrderCriteria){
            return ResultGenerator.generatorFail(WorkFlowStatus.PARAM_IS_EMPTY);
        }
        if (0 < workOrderService.maChargeAccept(workOrderCriteria)){
            return ResultGenerator.generatorSuccess();
        }else{
            return ResultGenerator.generatorFail(WorkFlowStatus.OTHER_EXCEPTION);
        }
    }
    /**
     *  桩维护审核拒绝
     *
     * @author ZhangMumu
     * @date 2018/3/31 0031 17:16
     * @param workOrderCriteria
     * @return
     */
    @ApiOperation(value = "桩维护审核拒绝,参数:工单uuid")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uuid", value = "工单uuid", dataType = "string", paramType = "query", required = true),
            @ApiImplicitParam(name = "reviewSuggestion", value = "审核时填写的备注", dataType = "string", paramType = "query")
    })
    @PostMapping("/maChargeReject")
    public ResponseResult maChargeReject(WorkOrderCriteria workOrderCriteria){
        if (null == workOrderCriteria){
            return ResultGenerator.generatorFail(WorkFlowStatus.PARAM_IS_EMPTY);
        }
        if (0 < workOrderService.maChargeReject(workOrderCriteria)){
            return ResultGenerator.generatorSuccess();
        }else {
            return ResultGenerator.generatorFail(WorkFlowStatus.OTHER_EXCEPTION);
        }
    }
    /**  
     *  统计工单数量  
     *   
     * @author ZhangMumu  
     * @date 2018/4/19 0019 16:26  
     * @param   
     * @return   
     */
    @ApiOperation(value = "统计工单数量")
    @PostMapping("/countWorkOrderNum")
    public ResponseResult countWorkOrderNum(){
        WorkOrderNumBean workOrderNumBean = workOrderService.countWorkOrderNum();
        if (null == workOrderNumBean){
            return ResultGenerator.generatorFail(BasicDataStatus.OTHER_EXCEPTION);
        }else {
            return ResultGenerator.generatorSuccess(workOrderNumBean);
        }
    }
}
